package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.DetailsToSeatInfoBO;
import cn.icanci.ladybird.pojo.dto.UserSeatSelectionDTO;
import cn.icanci.ladybird.pojo.entity.TicketSeat;
import cn.icanci.ladybird.service.TicketSeatService;
import cn.icanci.ladybird.service.user.UserSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: YPF
 * @date: Created in 2020/10/3 9:22
 * @classAction: 用户页面的座位页面
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/api/v1/pub/user")
public class UserSeatController {
    @Autowired
    private UserSeatService userSeatService;
    @Autowired
    private TicketSeatService ticketSeatService;

    @PostMapping("/seats")
    public String getUserSeatJson(Model model, DetailsToSeatInfoBO detailsToSeatInfoBO) {
        //不查询列表 只查询id，查询标题和价格
        ArrayList<Object> list = new ArrayList<>();
        // 获取标题
        list.add(detailsToSeatInfoBO.getTitle());
        // 获取 票的id
        list.add(detailsToSeatInfoBO.getTicketDetailsId());
        detailsToSeatInfoBO.getPrice().sort((p1, p2) -> {
            return p1.compareTo(p2);
        });
        // 获取升序的价格
        list.add(detailsToSeatInfoBO.getPrice());
        // 封装到json对象中区
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        return "user/seats";
    }

    @GetMapping("/seats-info")
    @ResponseBody
    public JsonData getUserSeatJson(Model model, Long detailsId) {
        System.out.println(detailsId);
        List<TicketSeat> ticketSeatList = ticketSeatService.queryManyByTicketDetailsId(detailsId);
        // 封装到json对象中区
        JsonData jsonData = JsonData.builderSuccess(ticketSeatList);
        return jsonData;
    }

    @ResponseBody
    @RequestMapping("/seats2")
    public JsonData getUserSeat2(Long ticketId) {
        List<UserSeatSelectionDTO> userSeatSelectionDTOS = userSeatService.userSeatSelectionDto(ticketId);
        return JsonData.builderSuccess(userSeatSelectionDTOS);
    }

    @Deprecated
    @RequestMapping("/seats3")
    public String getUserSeatJson3(Model model, DetailsToSeatInfoBO detailsToSeatInfoBO) {
        //查询列表
        System.err.println(detailsToSeatInfoBO);
        List<UserSeatSelectionDTO> userSeatSelectionDTOS = userSeatService.userSeatSelectionDto(detailsToSeatInfoBO.getTicketDetailsId());
        List<TicketSeat> ticketSeatList = ticketSeatService.queryManyByTicketDetailsId(detailsToSeatInfoBO.getTicketDetailsId());
        ArrayList<Object> list = new ArrayList<>();
        list.add(userSeatSelectionDTOS);
        list.add(ticketSeatList);
        list.add(detailsToSeatInfoBO.getTitle());
        list.add(detailsToSeatInfoBO.getTicketDetailsId());
        detailsToSeatInfoBO.getPrice().sort((p1, p2) -> {
            return p1.compareTo(p2);
        });
        list.add(detailsToSeatInfoBO.getPrice());
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        return "user/seats";
    }
}
